/* File: GeneActionStrategy.java
 * Purpose: Superclass for all algorithms the organisms use to make 'decisions'.
 * Author: Team Karp
 * Date: 12/13/07
 * Revisions: 12/13/07 - Initial Version - Adam Patterson
 *          [2008-01-22, Adam Patterson - parse now throws ScriptException]
 * Additional Requirements: 
 */
package protovitae.data;

import java.util.ArrayList;
import javax.script.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @version 1.0
 * @created 06-Dec-2007 16:19:09
 */
public abstract class GeneActionStrategy implements XMLSavable {

    protected String code;
    protected Invocable actionScript = null;
    protected ArrayList<Byte> geneticInformation = new ArrayList<Byte>();

    /**
     * This method is called to execute the GeneActionStrategy.
     */
    public abstract Object execute(Object information, ConcreteOrganism parent);

    /**
     * 
     * @param parent
     */
    public void save(Element parentElm, Document doc) {
        
    }

    /**
     * This parses the code string and stores it in param code.
     * 
     * @param code
     */
    public void parse(String code) throws ScriptException {

    }

    /** Get the JavaScript source code for this GeneActionStrategy.
     * @return The JS Source code
     **/
    public String getCode() {
        return code;
    }
    
    
    public byte[] getGeneticInformation() {
        byte[] ba = new byte[geneticInformation.size()];
        for (int i = 0; i < geneticInformation.size(); ++i) {
            ba[i] = geneticInformation.get(i);
        }
        return ba;
    }
    


    /**
     * This converts the variables in the code into a string for genes to use and
     * mutate with during the mating process
     * @return String of the code's variables in CSV format
     */
    public String getVariables() {
        StringBuffer varString = null; //String that will hold all the variables and separated by commas, so in CSV format
        int counter = 0; //keeps track of where you are in the code
        while(counter < code.length()) { //while there is more of the code to look at
            int fndpt = code.indexOf("var", counter); //find a variable to grab data from, from the point the counter last searched
            int eqlpt = code.indexOf("=", fndpt); //find where the variables's information exists
            varString.append(code.substring(eqlpt, code.indexOf(";", eqlpt))); //puts the data into the string
            counter = code.indexOf(";", eqlpt); //sets the counter to the next value to search from
            varString.append(","); //comma separates values
        }
        varString.deleteCharAt(varString.length()); //removes last comma, so that when the info is handled later, info is grabbed where it doesn't exist
        return varString.toString();
    }
    /** Set the JavaScript code to execute for this Gene Action Strategy
     * @param code JavaScript code to execute
     */
/*    public boolean setCode(String code) {
        boolean retval = checkCode(code);
        if(retval)
            this.code = code;
        return retval;
    }*/
    
    /** Validate the JavaScript code passed into this function to ensure that it is valid for this strategy.
     * It has been placed in the strategies themseleves instead of the editor, because we could
     * possibly make use of it upon loading a gene from XML, in case a used modifies its code directly
     * in the saved file with something invalid.
     * @param code The code to check
     * @return True if the code is valid, false if it will not execute.
     */
/*    public boolean checkCode(String code){
        return code!=null; //Shoud usually return true. Do we want generic validation?
        //See the actual strategies for a real implementation of this.
    }*/
}
